<!DOCTYPE html>
<HTML>
<HEAD>
    <TITLE> 权限分配 </TITLE>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="/resources/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
    <script type="text/javascript" src="/resources/zTree_v3/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="/resources/zTree_v3/js/jquery.ztree.all.js"></script>
</HEAD>
<BODY>
<input type="hidden" id="roleId">
<div>
    <ul id="treeDemo" class="ztree"></ul>
</div>
<SCRIPT LANGUAGE="JavaScript">
    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        data:{
            simpleData:{
                enable:true,
                idKey: "id",
                pIdKey: "pid",
            }
        },
        check:{

            chkStyle: "checkbox",//复选框类型
            enable: true //每个节点上是否显示 CheckBox
        },
        view:{
            dblClickExpand: true,//双击节点时，是否自动展开父节点的标识
            showLine: true,//是否显示节点之间的连线
            fontCss:{'color':'black'},//字体样式函数
            selectedMulti: false //设置是否允许同时选中多个节点
        }
    };
    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
    var zNodes = [
        {name:"test1", open:true, children:[
            {name:"test1_1"}, {name:"test1_2"}]},
        {name:"test2", open:true, children:[
            {name:"test2_1"}, {name:"test2_2"}]}
    ];
    $(document).ready(function(){
        $.ajax({
            url:'/permission/selectAllPermission',
            success:function (res) {
                console.log(res.data.permissions);
                zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, res.data.permissions);
                //勾选复选框
                getChecked();
            }
        });
        //勾选方法
        function getChecked() {
            $.ajax({
                url:'/permission/getChecked',
                data:{rid:$("#roleId").val()},
                success:function (res) {
                    var perIds=res.data.permissionIds;
                    if(perIds.length>0){
                        for(var i=0;i<perIds.length;i++){
                            var node=zTreeObj.getNodeByParam("id",perIds[i]);
                            zTreeObj.checkNode(node,true);

                        }
                    }
                }
            })
        }
    });
    //回调函数
    var callbackdata=function () {
        //获取勾选集合
        var checkedNodes=zTreeObj.getCheckedNodes(true);
        var perIds=new Array();
        if(checkedNodes!=null){
            if(checkedNodes.length>0){
                for(var i=0;i<checkedNodes.length;i++){
                    perIds.push(checkedNodes[i].id)
                }
            }
        }
        return{rid:$('#roleId').val(),perIds:perIds};
    }
</SCRIPT>
</BODY>
</HTML>